<?php
class FrontController
{
	public $baseurl;
	public $roordir;
	public static $instance=null;
	
	public function __construct()
	{
		if(self::$instance!==null)
		{
			throw new Exception("Registry is created!");
		}
	}
	public static  function getInstance()
	{
		if(self::$instance===null)
		{
			$class=__CLASS__;
			self::$instance=new $class();
		}
		return self::$instance;
	}
	public function getBaseUrl()
	{
		if(empty($this->baseurl))
		{
			$baseurl=dirname($_SERVER['SCRIPT_NAME']);
			if(strlen($baseurl)-1!=strripos($baseurl,'/'))
			{
				$baseurl.='/';
			}
			$this->baseurl=$baseurl;
		}
		return $this->baseurl;
	}
	
	public function getRootDir()
	{
		if (empty($this->roordir)) 
		{
			$rootdir=dirname($_SERVER['SCRIPT_FILENAME']);
			if(strlen($rootdir)-1!=strripos($rootdir,"/"))
			{
				$rootdir.="/";
			}
			$this->roordir=$rootdir;
		}
		return $this->roordir;
	}
	
	public function execute()
	{
		$request_uri=parse_url($_SERVER['REQUEST_URI']);
		if($request_uri["path"]==BASEURL."amfphp/gateway.php")
		{
			include(ROOTDIR."amfphp/gateway.php");
			return ;
		}
		$this->getRedirectParam();
		$registry=Registry::getInstance();
		$route=$registry->get("route");
		$module=$route->getModuleName();
		$controller=$route->getControllerName();
		$action=$route->getActionName();
		$controller_file=$this->getRootDir()."application/modules/".$module."/controller/".strtolower($controller)."controller.php";
		if(!file_exists($controller_file))
		{
			throw new Exception("Not Found Controller $controller");
		}
		require($controller_file);
		$controller_name=$controller."Controller";
		$controller=new $controller_name();
		$action=$action."Action";
		if(method_exists($controller,"init")&&is_callable(array($controller_name,"init")))
		{
			$controller->init();
		}
		if(method_exists($controller,$action)&&is_callable(array($controller_name,$action)))
		{
			$param=$this->getRedirectParam();
			$eval='$controller->$action(';
			if(!empty($param))
			{
				foreach ($param as $value)
				{
					$eval.="'".$value."',";
				}
				$eval=substr($eval,0,strlen($eval)-1);
			}
			$eval.=');';
			eval($eval);
			//$controller->$action();
		}
		else 
		{
			throw new Exception("The controller $controller_name's action $action is not found or is not callable");
		}
	}
	
	public function getRedirectParam()
	{
		$request_uri=$_SERVER['REQUEST_URI'];
		$request_uri=substr($request_uri,strlen($this->getBaseUrl()));		
		if(is_int(strpos($request_uri,"?")))
		{
			$request_uri=substr($request_uri,0,strpos($request_uri,"?"));
		}
		$request_uri=explode("/",$request_uri);
		
		$registry=Registry::getInstance();
		$route=$registry->get("route");
		$module=$route->getModuleName();
		$controller=$route->getControllerName();
		$action=$route->getActionName();
	    if(@strtolower($request_uri[0])==strtolower($module))
	    {
	    	array_shift($request_uri);
	    }
		if(@strtolower($request_uri[0])==strtolower($controller))
		{
			array_shift($request_uri);
		}
		if(@strtolower($request_uri[0])==strtolower($action))
		{
			array_shift($request_uri);
		}
		$param=array();
		if(!empty($request_uri))
		{
			$request_uri[count($request_uri)-1]=basename($request_uri[count($request_uri)-1],URL_SUFFIX);
			foreach ($request_uri as $value)
			{
				if(preg_match("/^[\w\s]*$/",$value))
				{
					$param[]=$value;
				}
				else 
				{
					$param[]="";
				}
			}
		}
		return $param;
	}
}
